package com.lagou.demo.test;

import com.lagou.demo.pojo.Page;
import com.lagou.demo.pojo.Student;
import com.lagou.demo.pojo.User;
import com.lagou.demo.service.StudentService;
import com.lagou.demo.util.DruidUtil;
import org.junit.Test;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class test {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
    @Test
    public void test(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        List<User> list = new ArrayList<>();
        try {
           connection = DruidUtil.getConnection();
           String sql="select * from t_user";
           preparedStatement = connection.prepareStatement(sql);
           resultSet = preparedStatement.executeQuery();
           while (resultSet.next()){
               User user = new User();
               user.setId(resultSet.getInt("id"));
               user.setUserName(resultSet.getString("username"));
               user.setPassword(resultSet.getString("password"));
               list.add(user);
           }
            for (User u: list) {
                System.out.println(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }

    }

    @Test
    public void test1(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("admin", "password");
        user.setId(1);
        try {
            connection = DruidUtil.getConnection();
            String sql="insert  into t_user (id,username,password)values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,String.valueOf(user.getId()));
            preparedStatement.setString(2,user.getUserName());
            preparedStatement.setString(3,user.getPassword());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test2(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("rose", "123456");
        try {
            connection = DruidUtil.getConnection();
            String sql="delete from t_user where username= ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("删除成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test3(){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        int i=0;
        User user = new User("admin2", "654321");
        user.setId(4);
        try {
            connection = DruidUtil.getConnection();
            String sql="update t_user set password=? where username =? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getPassword());
            preparedStatement.setString(2,user.getUserName());
            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("修改成功！");
            }else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement);
        }
    }
    @Test
    public void test4(){
        StudentService studentService = new StudentService();
        List<Student> students = studentService.StudentSearch(new Page(1, 10));
        for (Student s:students) {
            System.out.println(s);
        }
        Date date = new Date();


    }
}
